// pages/owner/sub/list-detail.js
const APP = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageinfo: {
            title: '',
            amount: '',
            create_time: '',
            remark: '',
            status: '',
            is_read: 1,
        },
        targetId:0,
        approvalList: [],
        //显示附件
        uploadFileObj:{
            images:[],videos:[],audios:[],documents:[]
        },
        uploadRef:null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        //控制权限
        APP.appauth.setAuthSetting();
        
        if (options.tid) {
            this.setData({
                targetId: options.tid,
            }, function () {
                _this.loadPageInfo(null);
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    // ====================【逻辑区域】====================

    //加载数据
    loadPageInfo: function (completedcallback) {
        let _this = this;
        APP._post('/Dlist/detail', {
            id: this.data.targetId
        },false, function (resData) {
            //执行成功
            _this.setPageData(resData);
        }, function (res) {
            //执行失败
        }, function (res) {
            //执行完成
            completedcallback && completedcallback();
        });
    },
    //设置页面数据
    setPageData(data) {
        //详细信息
        let info = APP.apptool.getNullFilter(data);
        this.setData({
            pageinfo: {
                title: info.title,
                amount: info.amount,
                create_time: info.create_time,
                remark: info.remark,
                status: info.status,
                is_read: info.is_read,
            },
        });
        //加载附件
        let tmpImglist = [];
        for(let i=0; i<data.attachmentList.length;i++){
            tmpImglist.push({
                fileType:data.attachmentList[i].fileType,
                filePath:data.attachmentList[i].filePath,
                fileName:data.attachmentList[i].fileName,
                fileUrl:APP.appconfig.fwrootweb + data.attachmentList[i].filePath,
            });
        }
        this.doSetUploadFile(tmpImglist);
        //审批流程
        let approval = APP.apptool.getNullFilter(data.approvalList);
        let tmplist = [],
            item = null;
        for (let i = 0; i < approval.length; i++) {
            item = approval[i];
            tmplist.push({
                create_time: APP.apptool.getNullFilter(item.create_time, '--'),
                begin_time: APP.apptool.getNullFilter(item.begin_time, '--'),
                emd_time: APP.apptool.getNullFilter(item.emd_time, '--'),
                director: item.director,
                remark: APP.apptool.getNullFilter(item.comment, '--'),
            });
        }
        this.setData({
            approvalList: tmplist
        });
    },
    //设置组件引用
    bindUploadReffer(ref){
        this.data.uploadRef = ref.detail;
    },
    doSetUploadFile(attachmentList){
        if(this.data.uploadRef){
            this.data.uploadRef.data.setAttachmentFiles(attachmentList);
        }
    },
})